import Axios from "axios";

//本地电影图片
export function getPicByMovieId(movieId) {
  const baseUrl = 'http://117.72.73.157/:8080/pic/'
  //数据库中有数据
  return baseUrl + movieId + '.jpg'

}

export function getOnePic(movieId) {
  const baseUrl = 'http://117.72.73.157/:8080/pic/'
  //数据库中有数据
  return baseUrl + "copy" + movieId + '.jpg'

}
export function getUsername(user) {
  if (user.hasOwnProperty('userNickname') && user.userNickname) {
    return user.userNickname;
  } else {
    let name = "用户"
    if (user.userPhone) {
      name += user.userPhone.slice(0, 8)
    }
    else if (user.userEmail) {
      name += user.userEmail.slice(0, 8)
    }
    else {
      name = ''
    }
    return name

  }
}

export function getAddress(location) {
  return new Promise((resolve, reject) => {
    let pos = `${location.longitude},${location.latitude}`;
    const data = {
      key: "d2cef2b6bc02a1af072e22fa50aaf17c",
      location: pos
    };

    Axios.get('https://restapi.amap.com/v3/geocode/regeo', { params: data }).then(res => {
      if (res.data.status == '1') {
        let half_address = res.data.regeocode.addressComponent.province +
          res.data.regeocode.addressComponent.city +
          res.data.regeocode.addressComponent.district +
          res.data.regeocode.addressComponent.township;
        let full_address = res.data.regeocode.formatted_address;
        let position = {
          longitude: location.longitude,
          latitude: location.latitude,
          half_address: half_address,
          full_address: full_address
        };
        localStorage.setItem('position', JSON.stringify(position));
        resolve(position);
      } else {
        reject(new Error('Failed to get address from API'));
      }
    }).catch(err => {
      reject(err);
    });
  });
}

/**
 * 根据name传回对应的项
 * @param {string} type
 * @returns 对应的icon和color
 */
export function getIcon(type) {
  if (!type) {
    return { name: '餐饮美食', icon: 'icon-canyinmeishi', color: '#FF7F50' }
  }
  const typeList = [
    { name: '餐饮美食', icon: 'icon-canyinmeishi', color: '#FF7F50' },  // coral
    { name: '休闲玩乐', icon: 'icon-xiuxianwanle2', color: '#6495ED' },  // cornflower blue
    { name: '看病买药', icon: 'icon-kanbingmaiyao', color: '#FFD700' },  // gold
    { name: '超市便利', icon: 'icon-fuwuchaoshi', color: '#98FB98' },  // pale green
    { name: '运动健康', icon: 'icon-yundong1', color: '#FFA07A' },  // light salmon
    { name: '便捷生活', icon: 'icon-bianjie_fangbian', color: '#20B2AA' },  // light sea green
    { name: '蔬菜水果', icon: 'icon-shucaishuiguo1', color: 'green' },  // aquamarine
    { name: '服装礼品', icon: 'icon-lipinka', color: '#87CEEB' },  // sky blue
    { name: '家居家装', icon: 'icon-shafa', color: '#FFB6C1' },  // light pink
    { name: '美容美发', icon: 'icon-meirong1', color: '#FFA500' },  // orange
    { name: '母婴用品', icon: 'icon-muying1', color: '#BA55D3' },  // medium orchid
    { name: '电子产品', icon: 'icon-dianzichanpin', color: '#CD5C5C' }  // indian red
  ]
    ;

  // 找到并返回匹配的类型对象
  return typeList.find(item => item.name === type);
}

/**
 * 根据商户类别决定商户经营场所
 * @param {string} type - 父类的名字
 * @returns {string[]} - 对应的子类别列表
 */
export function getChildList(type) {
  const childList = [
    { parent: '餐饮美食', child: ['中餐', '西餐', '早餐', '快餐', '饮品', '面包甜点'] },
    { parent: '休闲玩乐', child: ['KTV', '电影院', '足疗', '游乐场', '电玩城', '桌游密室'] },
    { parent: '看病买药', child: ['医院', '诊所', '药店', '体检中心', '康复中心', '中医'] },
    { parent: '超市便利', child: ['超市', '便利店', '食品杂货', '日用品', '生鲜', '饮料'] },
    { parent: '运动健康', child: ['健身房', '瑜伽', '游泳', '羽毛球', '篮球', '足球'] },
    { parent: '便捷生活', child: ['家政服务', '搬家', '维修', '干洗', '快递', '宠物服务'] },
    { parent: '蔬菜水果', child: ['蔬菜', '水果', '有机蔬果', '干果', '冷冻蔬果', '鲜榨果汁'] },
    { parent: '服装礼品', child: ['服装', '鞋帽', '珠宝首饰', '礼品店', '手表', '箱包'] },
    { parent: '家居家装', child: ['家具', '家电', '装修公司', '装饰品', '建材', '园艺'] },
    { parent: '美容美发', child: ['美容院', '美发店', '美甲店', 'SPA', '化妆品', '护肤品'] },
    { parent: '母婴用品', child: ['婴儿服装', '奶粉', '尿布', '婴儿玩具', '婴儿车', '孕妇用品'] },
    { parent: '电子产品', child: ['手机', '电脑', '平板', '相机', '耳机', '智能家居'] }
  ];
  let index = childList.findIndex(item => {
    return item.parent == type
  })
  if (index >= 0) {
    return childList[index].child
  }
  else {
    return []
  }
}


//用户头像处理
export function getAvartar(url) {
  //const baseUrl = 'http://192.168.151.49:8080/pic/'
  const baseUrl = 'http://localhost:9090/pic/'
  //数据库中有数据
  if (url) {
    return baseUrl + url
  }
  else {
    return "../../static/imgs/avartar.jpg"
  }
}

//商户选择头像时的处理
export function getAvartar2(url) {
  //const baseUrl = 'http://192.168.151.49:8080/pic/'
  const baseUrl = 'http://localhost:9090'
  //数据库中有数据
  if (url) {
    return baseUrl + url
  }
  else {
    return "../../static/imgs/choose.png"
  }
}


//商品单个图片的处理
export function getAvartar3(url) {
  //const baseUrl = 'http://192.168.151.49:8080/pic/'
  const baseUrl = 'http://localhost:9090'
  //数据库中有数据
  if (url) {
    return baseUrl + url
  }
  else {
    return "../../static/imgs/1.jpg"
  }
}


//商品图片列表的处理
export function getAvartarList(list) {
  //const baseUrl = 'http://192.168.151.49:8080/pic/';
  const baseUrl = 'http://localhost:9090';
  //数据库中有数据
  return list.map(element => baseUrl + element);
}
export function getMerchantAvartar(url) {
  //const baseUrl = 'http://192.168.151.49:8080/pic/'
  const baseUrl = 'http://localhost:9090'
  //数据库中有数据
  if (url) {
    return baseUrl + url
  }
  else {
    return "../../static/imgs/1.jpg"
  }
}

export function getPosition() {
  let position = {
    latitude: 0,
    longitude: 0,
    half_address: '',
    full_address: ''
  };

  return new Promise((resolve, reject) => {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        // locationSuccess 获取成功的话
        function (res) {
          position.longitude = res.coords.longitude.toFixed(6);
          position.latitude = res.coords.latitude.toFixed(6);
          localStorage.setItem('position', JSON.stringify(position))
          resolve(position);
        },
        // locationError 获取失败的话
        function (error) {
          handleLocationError(error);
          reject(error);
        }
      );
    } else {
      console.log("不支持");

      // alert('您的浏览器不支持地理位置获取');
      // reject(new Error('Geolocation not supported'));
    }
  });
}

function handleLocationError(error) {
  console.log(error);

  // const errorMessages = [
  //   '您拒绝共享位置信息',
  //   '获取不到位置信息',
  //   '获取位置信息超时'
  // ];
  // alert(errorMessages[error.code - 1] || '未知错误');
}
